"""
初始化默认数据的脚本
"""
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from models.category import Category
from database import Base, engine
from config import settings

def init_default_categories():
    """初始化默认分类"""
    # 使用现有的engine
    Session = sessionmaker(bind=engine)
    session = Session()
    
    # 检查是否已有分类
    existing_categories = session.query(Category).all()
    if existing_categories:
        print("分类数据已存在，跳过初始化")
        session.close()
        return
    
    # 定义默认分类
    default_categories = [
        Category(name="随笔", description="日常随笔、随想", color="#409EFF"),
        Category(name="技术", description="技术文章、编程相关", color="#67C23A"),
        Category(name="生活", description="生活记录、分享", color="#E6A23C"),
        Category(name="博客", description="博客相关", color="#F56C6C"),
    ]
    
    # 添加到数据库
    for category in default_categories:
        session.add(category)
    
    session.commit()
    
    # 在关闭会话前获取所有分类的名称
    category_names = [c.name for c in default_categories]
    session.close()
    print("默认分类初始化成功：", category_names)

if __name__ == "__main__":
    print("开始初始化默认分类...")
    init_default_categories()
    print("初始化完成！")

